const express = require('express'),
  router = express.Router(),
  util = require("../util/util"),
  book = require("../model/book"),
  chapter = require("../model/chapter");
    price = require("../model/price");

let reObj = util.reObj;


/**
 * @function 添加信息
 * @author Eric
 * @date 2018-08-01
 * @param bookid,chpaterid,type,price
 */
router.post('/add', async function(req, res)  {
    let bookid=req.body.bookid;
    let queryparam = {
      bookid:bookid
    }
    let bookname=""
    let chaptername=""
    try {
      let bookObj=await book.findOne({where: {id:bookid}})
    if (bookObj)
    {
        bookname=bookObj.name        
    }
    else
    {
      res.send(reObj(0, '该书籍不存在', {}))
    }
    if(req.body.chapterid)
    {
      let chapterObj=await chapter.findOne({where: {id:req.body.chapterid}})          
      chaptername=chapterObj.name                   
      
    }
    
    req.body.bookid=bookid
    req.body.bookname=bookname
    req.body.chaptername=chaptername
    let record=await price.findOrCreate({where: queryparam,defaults:req.body})
    if (record[1]) {
     // await chapter.update({price:parseFloat(req.body.price)},{where: {id:req.body.chapterid}})     
      res.send(reObj(0, '添加成功', record[0]))
  } else {
      res.send(reObj(-1, '记录已存在'))
  }
    } catch (error) {
      res.send(reObj(-1, '添加失败', error))
    }
    

})
router.post('/chapteradd', async (req, res) => {
  let chapterid=req.body.chapterid;
  let queryparam = {
      chapterid:chapterid
  }
  try {
    let priceObj=await price.findOrCreate({where: queryparam,defaults: req.body})  
    if (priceObj[1]) {
      if(req.body.chapterid)
      console.log("hahahahahahah"+req.body.chapterid)
{
  
  let chapterObj=await chapter.findOne({where: {id:req.body.chapterid}})       
  await chapter.update({price:parseFloat(req.body.price)},{where: {id:req.body.chapterid}})            
}
        res.send(reObj(0, '添加成功', priceObj[0]))
    } else {
        res.send(reObj(-1, '记录已存在'))
    }
  } catch (error) {
    res.send(reObj(-1, '添加失败', error))
  }
  
  

})

/**
 * @function 编辑校区信息
 * @author Eric
 * @date 2018-08-01
 * @param bookid,chpaterid,type,price
 */
router.post('/update', async (req, res) => {
  let id = req.body.id
  let params = {
    id: id
  }
  let bookname=""
  let chaptername=""
  /* let bookObj=await book.findOne({where: {id:bookid}}) */
  /* if (bookObj)
  {
      bookname=bookObj.name        
  }
  else
  {
    res.send(reObj(0, '该书籍不存在', {}))
  } */
  if(req.body.chapterid)
  {
    let chapterObj=await chapter.findOne({where: {id:req.body.chapterid}})       
    await chapter.update({price:parseFloat(req.body.price)},{where: {id:req.body.chapterid}})   
    chaptername=chapterObj.name          
  }
  else
  {
    let priceRecord=await price.findOne({where: {id:req.body.id}}) 
    await book.update({price:parseFloat(req.body.price)},{where: {id:priceRecord.bookid}})
  }
  
  
  /* price.findOrCreate({
      where: queryparam,
      defaults: {bookid:bookid,bookname:bookname,chaptername:chaptername}
  }).then(record => {
      if (record[1]) {
          res.send(reObj(0, '添加成功', record[0]))
      } else {
          res.send(reObj(-1, '记录已存在'))
      }
  }).catch(err => {
      res.send(reObj(-1, '添加失败', err))
  }); */
    req.body.chaptername=chaptername        
    price.update(req.body, {where: params}).then(record => {
        if (record[0] > 0) {
            
            res.send(reObj(0, '修改成功', record[0]))
        } else {
            res.send(reObj(-1, '无效id', []))
        }
    }).catch(err => {
        res.send(reObj(-1, '修改失败', err))
    });

})

/**
 * @function 删除对应的会议数据
 * @author Eric
 * @date 2018-07-31
 * @param id
 */
router.post('/del', (req, res) => {
  let params = {
    id: req.body.id
  }

  price.destroy({where: params}).then(record => {
    if (record > 0) {
      res.send(reObj(0, '删除成功', record))
    } else {
      res.send(reObj(-1, '删除失败或此记录不存在', record))
    }
  }).catch(err => {
    res.send(reObj(-1, '删除会议失败', err))
  });
})

/**
 * @function 获取单个信息
 * @author Eric
 * @date 2018-08-01
 * @param meetingid
 */
router.post('/findone', (req, res) => {
  let params = {}
  let id = req.body.id
  if (id) {
    params.id = id;

    price.findOne({where: params}).then(record => {
      if (record) {
        res.send(reObj(0, '获取信息成功', record))
      } else {
        res.send(reObj(-1, '获取信息失败或此记录不存在', {}))
      }
    }).catch(err => {
      res.send(reObj(-1, '获取信息失败', err))
    });
  } else {
    res.send(reObj(-1, 'id不能为空', []))
  }
})


/**
 * @function 获取所有数据(支持分页，条件过滤)
 * @author Eric
 * @date 2018-07-31
 * @param page,pagerow,meetingname,meetingtype
 */
router.post('/findall', (req, res) => {
 /* let meetingname = req.body.meetingname || ''
  let meetingtype = req.body.meetingtype || ''*/
  let params = req.body
  let pagerow = parseInt(req.body.pagerow)
  let page = parseInt(req.body.page)
  if(pagerow){
    delete params.pagerow;
  }
  if(page){
    delete params.page;
  }
  //对页码进行容错
  if (pagerow > 999) {
    pagerow = 999
  }
  if (page == 0) {
    page = 1
  }


  //筛选条件
/*  if (meetingname) {
    params.meetingname = {'$like': '%' + meetingname + '%'}
  }
  if (meetingtype) {
    params.meetingtype = meetingtype
  }*/

  price.findAndCountAll({
    order: "creattime DESC",
    limit: pagerow || 1000,
    offset: parseInt(pagerow * (page - 1)) || 0,
    where: params
  }).then(record => {
    if(record.count>0){
      res.send(reObj(0, '获取数据成功', record.rows, record.count))
    }
    else
    {
      res.send(reObj(0, '没有符合条件的记录', record.rows, record.count))
    }
    
  }).catch(err => {
    res.send(reObj(-1, '获取数据失败', err))
  });
})


module.exports = router;
